package system

import (
	"context"

	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
)

type SysStoresService struct{}

// CreateSysStores 创建门店信息记录
// Author [yourname](https://github.com/yourname)
func (sysStoresService *SysStoresService) CreateSysStores(ctx context.Context, sysStores *system.SysStores) (err error) {
	err = global.GVA_DB.Create(sysStores).Error
	return err
}

// DeleteSysStores 删除门店信息记录
// Author [yourname](https://github.com/yourname)
func (sysStoresService *SysStoresService) DeleteSysStores(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&system.SysStores{}, "id = ?", ID).Error
	return err
}

// DeleteSysStoresByIds 批量删除门店信息记录
// Author [yourname](https://github.com/yourname)
func (sysStoresService *SysStoresService) DeleteSysStoresByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]system.SysStores{}, "id in ?", IDs).Error
	return err
}

// UpdateSysStores 更新门店信息记录
// Author [yourname](https://github.com/yourname)
func (sysStoresService *SysStoresService) UpdateSysStores(ctx context.Context, sysStores system.SysStores) (err error) {
	err = global.GVA_DB.Model(&system.SysStores{}).Where("id = ?", sysStores.ID).Updates(&sysStores).Error
	return err
}

// GetSysStores 根据ID获取门店信息记录
// Author [yourname](https://github.com/yourname)
func (sysStoresService *SysStoresService) GetSysStores(ctx context.Context, ID string) (sysStores system.SysStores, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&sysStores).Error
	return
}

// GetSysStoresInfoList 分页获取门店信息记录
// Author [yourname](https://github.com/yourname)
func (sysStoresService *SysStoresService) GetSysStoresInfoList(ctx context.Context, info systemReq.SysStoresSearch) (list []system.SysStores, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&system.SysStores{})
	var sysStoress []system.SysStores
	// 如果有条件搜索 下方会自动创建搜索语句
	if len(info.CreatedAtRange) == 2 {
		db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
	}
	if info.StoreName != "" {
		db = db.Where("store_name LIKE ?", "%"+info.StoreName+"%")
	}

	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&sysStoress).Error
	return sysStoress, total, err
}
func (sysStoresService *SysStoresService) GetSysStoresPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
